package cn.iocoder.yudao.module.pay.channel.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.pojo.dto.SimpleQueryDTO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.api.pay.order.eums.UnifiedPayChannelEnum;
import cn.iocoder.yudao.module.pay.channel.service.PayChannelService;
import cn.iocoder.yudao.module.pay.channel.vo.PayChannelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 App - 支付渠道")
@RestController
@RequestMapping("/pay/channel")
@Validated
public class AppPayChannelController {

    @Resource
    private PayChannelService payChannelService;

    @GetMapping("/get-enable-channel-codes")
    @Operation(summary = "获得开启的支付渠道编码列表")
    @Parameter(name = "scene", description = "支付场景：default-默认、mall-商城、o2o-商城、recharge-充值", required = true, example = "default")
    @PermitAll
    public CommonResult<Set<String>> getEnableChannelCodeList(@RequestParam("scene") String scene) {
        List<PayChannelVO> channels = payChannelService.getList(new SimpleQueryDTO());
        if (CollUtil.isEmpty(channels)) {
            return success(Collections.emptySet());
        }
        channels = channels.stream().filter(PayChannelVO::getEnabled).toList();
        if (CollUtil.isEmpty(channels)) {
            return success(Collections.emptySet());
        }
        if (scene == null) {
            scene = "";
        }
        if (StrUtil.equalsIgnoreCase(scene, "recharge")) {
            channels = channels.stream().filter(channel -> !Arrays.asList(
                            UnifiedPayChannelEnum.WALLET.getCode(), UnifiedPayChannelEnum.MOCK.getCode(),
                            UnifiedPayChannelEnum.UNKNOWN.getCode())
                    .contains(channel.getCode())).toList();
        } else if (StrUtil.equalsIgnoreCase(scene, "mall")) {
            channels = channels.stream().filter(channel -> !Arrays.asList(
                            UnifiedPayChannelEnum.CASH.getCode(), UnifiedPayChannelEnum.MOCK.getCode(),
                            UnifiedPayChannelEnum.UNKNOWN.getCode())
                    .contains(channel.getCode())).toList();
        } else if (StrUtil.equalsIgnoreCase(scene, "o2o")) {
            channels = channels.stream().filter(channel -> !Arrays.asList(
                            UnifiedPayChannelEnum.MOCK.getCode(), UnifiedPayChannelEnum.UNKNOWN.getCode())
                    .contains(channel.getCode())).toList();
        } else {
            channels = channels.stream().filter(channel -> !Objects.equals(UnifiedPayChannelEnum.UNKNOWN.getCode(), channel.getCode())).toList();
        }
        return success(convertSet(channels, PayChannelVO::getCode));
    }

}
